<?php

namespace eu\freeplace\page;

use eu\freeplace\page\IDocument as IDocument;
use eu\freeplace\page\Head      as Head;
use eu\freeplace\page\Body      as Body;
use eu\freeplace\page\model\DocumentVO as DocumentVO;
use eu\freeplace\page\model\TemplateVO as TemplateVO;

/**
 * This Class implements the Main Functionality of a Document.
 *
 * @author Elmar Dott
 * @version 1.0
 */
class Document implements IDocument {

    /** Class Name */
    const CLASS_NAME = "Document";

    /**
     * Default Constructor
     * @global Logger $logger
     */
    public function __construct() {
        global $logger;
        $logger->log("Calling instance of ".self::CLASS_NAME."()", "NOTICE");
    }

    public function drawDocument($contentID) {
        global $logger;
        $logger->log(self::CLASS_NAME.".drawDocument(".$contentID.")", "DEBUG");

        $document = self::getDocument($contentID);
        $template = self::loadTemplate($document['DocumentType'], $document['ViewType']);
        $head     = self::getHead($document, $template);
        $content  = self::getBody($contentID, $head);
        return $content;
    }

    ### ########################################################################

    private function getDocument($contentID) {
        global $logger;
        $documentVO = clone new DocumentVO($contentID);
        $logger->log(self::CLASS_NAME."().getDocument() Document[ID:".$contentID."] successful fetched.", "DEBUG");
        return $documentVO->getClassAttributes();
    }

    private function loadTemplate($documentType, $viewType) {
        global $logger;
        $templateVO = clone new TemplateVO($documentType, $viewType);
        $logger->log(self::CLASS_NAME."().loadTemplate()", "DEBUG");
        $template = $templateVO->getClassAttributes();
        $logger->log("\tTEMPLATE:\n".$template['Template'], "DEBUG");
        return $template['Template'];
    }

    private function getHead($document, $template) {
        global $logger;
        $logger->log(self::CLASS_NAME."().getHead(".$document['ContentId'].")", "DEBUG");

        $html = $template;
        $head = new Head();
        $meta = $head->getMETA($document['ContentId']);
        $css  = $head->drawCSS();
        $js   = $head->drawJS();

        $logger->log("\tCSS:\n".$css, "DEBUG");
        $logger->log("\tJavaScript:\n".$js, "DEBUG");

        //REPLACEMENT
        $html = str_replace('@TITLE@',       $meta['Title'], $html);
        $html = str_replace('@DESCRIPTION@', $meta['Description'], $html);
        $html = str_replace('@KEYWORDS@',    $meta['Keywords'], $html);
        $html = str_replace('@SEARCHBOT@',   $meta['SearchBot'], $html);
        $html = str_replace('@DOMAIN@',      $meta['Domain'], $html);
        $html = str_replace('@LOCALE@',      $meta['Locale'], $html);

        $html = str_replace('@EDITOR@', $document['Editor'], $html);

        $html = str_replace('@CSS@', $css, $html);
        $html = str_replace('@JS@',  $js, $html);

        return $html;
    }

    private function getBody($contentID, $template) {

        global $logger;
        $logger->log(self::CLASS_NAME."().getBody(".$contentID.")", "DEBUG");

        $body = new Body();
        return str_replace('@BODY@',  $body->fetchContent($contentID), $template);
    }

}//CLASS

?>
